﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Zombie_Demo.Utilities;

namespace Zombie_Demo.Utilities
{
     public class Animation
     {
          protected string spriteSheetKey;
          protected Rectangle animBox;
          protected int x, y, startX, startY, endX, endY, spacingX, spacingY;
          protected int numWide, numHeight;
          protected double animSpeed, lastUpdate;
          protected bool paused;

          public bool Paused { get { return this.paused; } set { this.paused = value; } }

          public Animation()
          {
               //TODO: Implement
          }

          public Animation(string sprite, int startX, int startY, int endX, int endY, int numWide, int numHeight, int offsetX, int offsetY)
          {
               this.paused = false;
               this.spriteSheetKey = sprite;
               this.startX = startX;
               this.startY = startY;
               this.endX = endX;
               this.endY = endY;
               this.numWide = numWide;
               this.numHeight = numHeight;
               this.spacingX = offsetX;
               this.spacingY = offsetY;
               this.x = startX;
               this.y = startY;
               this.lastUpdate = 0.0;
               if (endY - startY == 0)
                    this.animSpeed = ((double)endX - startX) / (double)30;
               else if (endX - startX == 0)
                    this.animSpeed = ((double)endY - startY) / (double)30;
               else
                    this.animSpeed = ((double)(endX - startX) * (double)(endY - startY)) / (double)30; //TODO: 0-0 = *0 causing 0 fps

               this.animBox = new Rectangle(offsetX, offsetY, 0, 0);

               if ((endX - startX) == 0)
                    this.animBox.Width = ContentLibrary.GetTexture(sprite).Width;
               else
                    this.animBox.Width = ((ContentLibrary.GetTexture(sprite).Width - (spacingX * (endX - startX))) / ((endX - startX) + 1));

               if ((endY - startY) == 0)
                    this.animBox.Height = ContentLibrary.GetTexture(sprite).Height;
               else
                    this.animBox.Height = ((ContentLibrary.GetTexture(sprite).Height - (spacingY * (endY - startY)) / ((endY - startY) + 1)));
          }

          public Rectangle GetImage(GameTime gameTime)
          {
               if(!paused)
               {
                    if (lastUpdate > animSpeed)
                    {
                         lastUpdate = 0;
                         if (x >= numWide)
                         {
                              x = startX;
                              if (y >= numHeight)
                              {
                                   y = startY;
                              }
                              else
                                   y++;
                         }
                         else
                              x++;
                    }
                    else
                    {
                         lastUpdate += gameTime.ElapsedGameTime.Milliseconds;
                    }
               }
               int newX = ((x - 1) * animBox.Width) + ((x - 1) * spacingX);
               int newY = ((y - 1) * animBox.Height) + ((y - 1) * spacingY);

               Rectangle toReturn = new Rectangle(newX,
                    newY,
                    animBox.Width,
                    animBox.Height);
               return toReturn;
          }
     }
}
